<?php

namespace Biz\model\wiki;


use Doraemon\tools\ImgUrl;

class WikiBasic extends \Doraemon\pockets\datebase\BaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_store';

    const _DATABASE_ = 'store';

    const _TABLE_NAME = 'wiki_basic';

    const STATE_NORMAL = 0;//默认，待审核
    const STATE_ONLINE = 1;//上线
    const STATE_OFFLINE = 2;//下线
    const STATE_PASS = 3;//审核通过
    const STATE_NOT_PASS = 4;//审核不通过
    const STATE_EDIT_AUDIT = 5;//编辑待审核
    const STATE_DEL = 9;//已删除

    const ON_LINE = 1;//线上
    const OFFLINE = 2;//非线上

    const STATE_MAP = [
        self::STATE_NORMAL => '待审核',
        self::STATE_ONLINE => '上线',
        self::STATE_OFFLINE => '下线',
        self::STATE_PASS => '审核通过',
        self::STATE_NOT_PASS => '审核不通过',
        self::STATE_DEL => '已删除'
    ];
    const ADM_ALLOW_STATE = [
        self::STATE_ONLINE,
        self::STATE_PASS,
    ];

    const WIKI_BASIC_BRAND = 1;
    const WIKI_BASIC_STORE = 2;

    const WIKI_BASIC_TYPE = [
        1 => '品牌',
        2 => '门店',
    ];

    const WIKI_RECOMMEND_LIST_TYPE_BUY = 1; //用户点击过去购买 （喜欢）
    const WIKI_RECOMMEND_LIST_TYPE_FAV = 2;//用户点击过收藏 （收藏）
    const WIKI_RECOMMEND_LIST_TYPE_RANKING_INFO = 3; // 榜单信息
    const WIKI_RECOMMEND_LIST_TYPE_RECOMMEND = 4;//推荐 比如 388位住友想买
    const WIKI_RECOMMEND_LIST_TYPE_ALBUM = 5;//好物清单

    public static function admAuditStateDesc($basicState)
    {
        $desc = '';
        switch ($basicState) {
            case self::STATE_NORMAL:
                $desc = '待审核';
                break;
            case self::STATE_EDIT_AUDIT:
                $desc = '编辑待审核';
                break;
            case self::STATE_ONLINE:
            case self::STATE_PASS:
                $desc = '审核通过';
                break;
            case self::STATE_NOT_PASS:
                $desc = '审核不通过';
                break;
        }
        return $desc;
    }

    public static function getByWhere($where, $select = '*')
    {
        $info = self::get($select, $where);
        $info = self::formatInfo($info);
        return $info;
    }

    public static function selectByWhere($where, $select = '*')
    {
        $result = [];
        $list = self::select($select, $where);
        if (empty($list) || !is_array($list))
            return $result;
        foreach ($list as $row) {
            $info = self::formatInfo($row);
            $result[] = $info;
        }
        return $result;
    }

    public static function selectCountWhere($where)
    {
        $count = self::count('id', $where);
        return $count;
    }

    public static function insertData($data)
    {
        if (!isset($data['addtime']) || !$data['addtime']) {
            $data['addtime'] = date('Y-m-d H:i:s');
        }
        if (!isset($data['edittime'])) {
            $data['edittime'] = date('Y-m-d H:i:s');
        }
        $ret = self::insert($data);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return self::id();
    }


    public static function updateData($data, $where)
    {
        if (!isset($data['edittime'])) {
            $data['edittime'] = date('Y-m-d H:i:s');
        }
        $ret = self::update($data, $where);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return true;
    }


    public static function getInfosByBasicIds($basicIds, $fields = '*')
    {
        $result = [];
        $where = [
            'id' => $basicIds,
            'basic_state[!]' => self::STATE_DEL
        ];
        $list = self::selectByWhere($where, $fields);
        foreach ($list as $row) {
            $result[$row['id']] = $row;
        }
        return $result;
    }

    /**
     * 通过basicId 获取basic信息
     * @param $basicId
     * @param string $fields
     * @return array|bool
     */
    public static function getInfosById($basicId, $fields = '*')
    {
        $where = [
            'id' => $basicId,
            'basic_state[!]' => self::STATE_DEL
        ];
        return self::getByWhere($where, $fields);
    }


    /**
     * 通过basicIds 获取basic信息
     * @param $basicId
     * @param string $fields
     * @return array
     */
    public static function getInfosByIds($basicId, $fields = '*'): array
    {
        $where = [
            'id' => $basicId,
            'basic_state[!]' => self::STATE_DEL
        ];
        return self::selectByWhere($where, $fields);
    }

    /**
     * wiki_basic图片格式化
     * @param $info
     * @return mixed
     */
    public static function formatInfo($info)
    {
        if (!empty($info)) {
            if (isset($info['cover_img'])) {
                $info['cover_img_url'] = !empty($info['cover_img']) ? ImgUrl::getUrl(ImgUrl::APPTYPE_BIZ, $info['cover_img'], ImgUrl::IMAGE_SIZE_ORI_NPHONE) : '';
            }
            if (isset($info['imgs'])) {
                $imgs = !empty($info['imgs']) ? json_decode($info['imgs'], true) : [];
                $info['img_urls'] = [];
                $tmp = [];
                if ($imgs) {
                    foreach ($imgs as $img) {
                        $tmp[] = $img['img'];
                        if ($img['status'] != \Biz\service\wiki\WikiBasic::WIKI_BASIC_IMAGE_STATUS_UNPASS) {
                            $info['img_urls'][] = ImgUrl::getUrl(ImgUrl::APPTYPE_BIZ, $img['img'], ImgUrl::IMAGE_SIZE_ORI_NPHONE);
                        }
                    }
                }
                $info['imgs'] = $tmp;
                $info['imgs_initial'] = $imgs;
            }
        }
        return $info;
    }


    /**
     * 格式化wiki_basic图片
     * @param $imgs
     * @return array|mixed
     */
    public static function formData($imgs)
    {
        $lists = $imgs ? json_decode($imgs, true) : [];
        foreach ($lists as &$list) {
            $list['img_id'] = $list['img'];
            $list['img'] = ImgUrl::getUrl(ImgUrl::APPTYPE_BIZ, $list['img'], ImgUrl::IMAGE_SIZE_ORI_NPHONE);
        }
        return $lists;
    }
}